<html><head>
<script type="text/javascript">
	var el, fel

	function showIt(elID) {
		if (fel && fel.length > 0) {
			for (x = 0; x < fel.length; x++)
				fel[x].style.visibility = 'hidden';
		}
		el = document.getElementById(elID);
		fel = document.getElementsByName(elID + "_frame");
		el.scrollIntoView(false);
		for (x = 0; x < fel.length; x++)
			fel[x].style.visibility = 'visible';
	}
</script>

<STYLE type="text/css">
	html body {margin:0px;padding:0px;}
	.dealer {float:left; white-space:nowrap; padding-top:0; display: inline; font-size: 0;}
	img.base-image{z-index:1; position:relative; border: 0;}
	img.overlay-image{position:absolute; z-index:10; visibility:hidden;}
	img.placeholder-image{z-index:0; position:relative; border: 0;}
	div.floating-menu {position:fixed;background:#fff4c8;border:1px solid #ffcc00;width:338x;z-index:100;}
	div.floating-menu a, div.floating-menu h3 {display:block;margin:0 0.5em;}
</style></head>
<body><div class="dealer">

<?php
require('../globals.php');

// This is a different order than the one in globals.php
// Why 'K' comes early in that list I have NO idea
$table_rows = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O');
$reserved_tables = array();

if (!($link = mysql_connect($host, $user, $password)))
	die('Could not connect: ' . mysql_error());

if (!(mysql_select_db($database)))
	die('Could not select db ' . $database . ': ' . mysql_error());

$layout_query = "SELECT d.dealership_name, t.row_number, d.table_size_req,  t.row_a_assignment, t.row_b_assignment
	from " . $dealers_table . " d, " . $tables_table . " t
	where d.cancelled = 0 and (t.row_a_assignment = d.dealer_id OR t.row_b_assignment = d.dealer_id)";
$reserved_query = "SELECT row_number from " . $tables_table . " where (row_a_assignment = '999' || row_b_assignment = '999')";
//$layout_query_select_menu = "SELECT dealership_name from " . $dealers_table . " WHERE cancelled = 0 order by dealership_name";

$results = mysql_query($layout_query, $link);
$reserved_results = mysql_query($reserved_query, $link);
//$results_selector = mysql_query($layout_query_select_menu, $link);
$results_selector = mysql_query($layout_query, $link);

while ($value = mysql_fetch_row($results_selector)) {
	if (substr($value[0],0,3) == 'The')	// quick hack to sort more naturally
		$clean_selector[$i] = substr($value[0],4,strlen($value[0])) . ", The";
	else
		$clean_selector[$i] = $value[0];
	$i++;
}

natcasesort($clean_selector);

?><div class="floating-menu">
<select size="1" name="jumpTo" onchange=showIt(this.value)>
<option value=""></option>
<?php
	foreach ($clean_selector as $key)
		echo("<option value=\"" . strtolower(str_replace($to_remove,"", $key)) . "\">$key</option>
			");
?></select></div><p style="padding:20px;"><?php

$left = 0;
$top = 50;

// step through the entire room and lay down 'base' tiles to mark empty positions
for ($i = 0; $i < count($table_rows); $i++) {
	$left = $i * 350;
	if ($i > 4 && $i < 9)
		$start_row = 16;
	else
		$start_row = 1;
	for ($j = $start_row; $j<= 20; $j++) {
		$top = $j * 100;
		if ($j <= 9)
			$table = $table_rows[$i] . 0 . $j;
		else
			$table = $table_rows[$i] . $j;
		$table_type = 'table';
		if ($i == 0 || $i == 13) {
			$table_type = 'wall';
			if (($j > 5 && $j < 12)	|| ($i == 0 && ($j < 5 && $j > 16)))
				$table_type .= '_power';
		}
		if (($i == 2 || $i == 3 || $i == 9 || $i == 12) && ($j > 5 && $j < 16))
			$table_type = 'power';
		?><img src="../images/tables/table_<?=$table ?>a.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top ?>px;">
		<img src="../images/tables/table_<?=$table ?>b.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top+50 ?>px;">
		<img class="placeholder-image" src="../images/open_<?=$table_type ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;">
		<img class="placeholder-image" src="../images/open_<?=$table_type ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;">		<?php
	}
}

// put Islands into position
for ($i = 1; $i < 7; $i++) {
	$left = 2100;
	$top = (100 * $i);
		?><img src="../images/tables/table_I0<?=$i ?>a.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top ?>px;">
		<img src="../images/tables/table_I0<?=$i ?>b.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top+50 ?>px;">
		<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;">
		<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
}

// put endcaps into place
for ($i = 1; $i < count($table_rows) - 1; $i+=2) {
	$left = 525 + (350 * ($i - 1));
	if ($i < 5 || $i > 8) {
		$top = 0;
		$interim = '00';
	}
	else {
		$top = 1500;
		$interim = '15';
	}
	?><img src="../images/tables/table_<?=$table_rows[$i] . $interim . $table_rows[$i+1] ?>a.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top ?>px;">
	<img src="../images/tables/table_<?=$table_rows[$i] . $interim . $table_rows[$i+1] ?>b.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top+50 ?>px;">
	<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+55 ?>px;top:<?=$top ?>px;">
	<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+55 ?>px;top:<?=$top+50 ?>px;">
	<img src="../images/tables/table_<?=$table_rows[$i] ?>21<?=$table_rows[$i+1] ?>a.png" style="position:absolute;left:<?=$left ?>px;top:2100px;">
	<img src="../images/tables/table_<?=$table_rows[$i] ?>21<?=$table_rows[$i+1] ?>b.png" style="position:absolute;left:<?=$left ?>px;top:2150px;">
	<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+55 ?>px;top:2100px;">
	<img class="placeholder-image" src="../images/open_table.png" style="position:absolute;left:<?=$left+55 ?>px;top:2150px;"><?php
}

// and now, actually put the assigned table tags in place
while ($values = mysql_fetch_row($results)) {
	$position = $values[1];
	if (array_search($position, $reserved_tables))
		$name = 'reserved';
	else {
		$name = strtolower(str_replace($to_remove,"",$values[0]));
	}
	if (strlen($position) <= 3) {			// endcaps need special handling
		if (substr($position,0,1) == 'I')	// so do islands
			$column = '6';
		else
			$column = array_search(substr($position,0,1), $table_rows);
		$row = substr($position,1,2);
		$left = ($column * 350);
		$top = ($row * 100);
		switch($values[2]) {
			case (0):		// Half table
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				?><img class="base-image" id="<?=$name ?>" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				if ($values[4] != '000') {	// A assigned, B open (reverse should not happen)
					$values = mysql_fetch_row($results);
					$name = strtolower(str_replace($to_remove,"",$values[0]));
					?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
					?><img class="base-image" id="<?=$name ?>" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
				}
				break;
			case (1):	// Full table
			case (4):	// Islands
			case (5):	// "Special" table (c-shape or quad)
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_top.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_bottom.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
				?><img class="base-image" id="<?=$name ?>" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;<?=$position ?>"><?php
				?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
				break;
			case (2):	// Double table
				$values = mysql_fetch_row($results);
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_top.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				?><img class="base-image" id="<?=$name ?>" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				for ($x = 0; $x < 2; $x++) {
					?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_sides.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
					?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				}
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_bottom.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				break;
			case (3):	// Triple
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_top.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				?><img class="base-image" id="<?=$name ?>" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
				for ($x = 0; $x < 4; $x++) {
					?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_sides.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
					?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				}
				?><img class="overlay-image" name="<?=$name ?>_frame" src="../images/frame_bottom.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50*($x+1) ?>px;"><?php
				for ($x = 0; $x < 3; $x++)
					mysql_fetch_row($results);
				break;
			default:
				break;
		}
	}
	else {	// Caps - assumption: caps are never sold as halfs (policy)
		$top = (substr($position,1,2) * 100);
		$left = ((array_search(substr($position,0,1), $table_rows)) * 350) + 230;
		?><img class="overlay-image" id="<?=$name ?>_frame" src="../images/frame.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top ?>px;"><?php
		?><img class="overlay-image" id="<?=$name ?>_frame" src="../images/frame.png" style="position:absolute;left:<?=$left+50 ?>px;top:<?=$top+50 ?>px;"><?php
		?><img class="base-image" src="../images/<?=$name ?>.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top ?>px;"><?php
		?><img class="base-image" src="../images/blank.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top+50 ?>px;"><?php
	}
}

// handling for reserved (assigned to 999) tables)
while ($value = mysql_fetch_row($reserved_results)) {
	$top = (substr($value[0],1,2) * 100);
	$left = ((array_search(substr($value[0],0,1),$table_rows) * 350) + 50);
	if (strlen($value[0]) == 4)
		$left += 180;
	?><img class="base-image" src="../images/reserved.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top ?>px;"><?php
	?><img class="base-image" src="../images/reserved.png" style="position:absolute;left:<?=$left ?>px;top:<?=$top+50 ?>px;"><?php
}

mysql_close($link);
?></p></div></body></html>